package de.baumann.browser.activitys.gg

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import de.baumann.browser.R
import de.baumann.browser.api.net.vo.gg.GGMain
import de.baumann.browser.base.BaseOdinActivity
import de.baumann.browser.db.ETHWallet
import de.baumann.browser.iview.gg.IGGMainView
import de.baumann.browser.present.gg.GGMainPresenter
import de.baumann.browser.views.dialog.DialogRiskTips
import kotlinx.android.synthetic.main.activity_g_main.*
import kotlinx.android.synthetic.main.include_tool_bar.*
import java.util.*

//odin测试链 https://o.n5.io:24570     gung的测试合约0x2fb24fDb2C02c7D07271A5690C0ba3623d37aA7d

class GMainActivity : BaseOdinActivity<IGGMainView, GGMainPresenter>(), IGGMainView {

    companion object {
        fun startGMainActivity(context: Context, wallet: ETHWallet) {
            context.startActivity(Intent(context, GMainActivity::class.java).apply {
                putExtras(Bundle().apply {
                    putSerializable("wallet", wallet)
                })
            })
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_g_main
    }

    override fun getTitleText(): String {
        return "Gungnir"
    }

    private var ggMain: GGMain? = null

    override fun initView() {
        initToolbar(commToolBar, commTitle)
        BecomeSmelter.setOnClickListener {
            if (ggMain == null) {
                initData()
                return@setOnClickListener
            }

            when (ggMain!!.type) {
                5, 6 -> {
                    CreateSmelterActivity.startCreateSmelterActivity(mContext, getWallet())
                }

                1 -> {
                    showShortToast("Creating")
                }

                2 -> {
                    showShortToast("Created")
                }

                3 -> {
                    showShortToast("Joining")
                }

                4 -> {
                    showShortToast("Joined")
                }
                7 -> {
                    showShortToast("Processing")
                }
            }
        }

        joinSmelter.setOnClickListener {
            if (ggMain == null) {
                initData()
                return@setOnClickListener
            }
            when (ggMain!!.type) {
                5, 6 -> {
                    JoinSmelterActivity.startJoinSmelterActivity(mContext, getWallet())
                }

                1 -> {
                    showShortToast("Creating")
                }

                2 -> {
                    showShortToast("Created")
                }

                3 -> {
                    showShortToast("Joining")
                }

                4 -> {
                    showShortToast("Joined")
                }

                7 -> {
                    showShortToast("Processing")
                }
            }

        }

        smelter.setOnClickListener {
            SmeltersActivity.startSmeltersActivity(mContext)
        }

        MyMortgage.setOnClickListener {
            if (ggMain?.type != 2) {
                MyMortgageActivity.startMyMortgageActivity(mContext, getWallet())
            } else {
                showShortToast("You Are Smelter")
            }
        }

        MySmelter.setOnClickListener {
            when (ggMain?.type) {
                2 -> {
                    MySmelterActivity.startMySmelterActivity(mContext, ggMain!!.smelteryId, getWallet())
                }
                else -> {
                    showShortToast("You haven't Smelter")
                }
            }
        }

        DialogRiskTips(mContext).show()
    }

    override fun initData() {

    }

    override fun onResume() {
        super.onResume()
        presenter?.a00(getWallet().address)
    }


    private fun getWallet(): ETHWallet {
        return intent.getSerializableExtra("wallet") as ETHWallet
    }

    override fun createPresenter(): GGMainPresenter {
        return GGMainPresenter()
    }

    override fun createView(): IGGMainView {
        return this
    }

    override fun setData(data: GGMain) {
        this.ggMain = data
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_gg_ransom, menu)
        return super.onCreateOptionsMenu(menu)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {

        if (ggMain == null) {
            return false
        }
        when (ggMain!!.type) {
            1 -> {
                showShortToast("Creating")
            }

            2 -> {
                if (ggMain == null) {
                    return false
                }
//                val calendar = Calendar.getInstance()
//                calendar.timeInMillis = ggMain!!.createTimeLong
//                calendar.add(Calendar.MONTH, 12)
//                val time = calendar.time.time
//                if (System.currentTimeMillis() > time) {
                    GRansomActivity.startGRansomActivity(this, ggMain!!, getWallet())
//                } else {
//                    showShortToast("can not ransom")
//                }
            }

            3 -> {
                showShortToast("Joining")
            }

            4, 5 -> {
                if (ggMain == null) {
                    return false
                }
//                val calendar = Calendar.getInstance()
//                calendar.timeInMillis = ggMain!!.createTimeLong
//                calendar.add(Calendar.MONTH, ggMain!!.month)
//                val time = calendar.time.time
//                if (System.currentTimeMillis() > time) {
                    GRansomActivity.startGRansomActivity(this, ggMain!!, getWallet())
//                } else {
//                    showShortToast("can not ransom")
//                }
            }

            6 -> {
                showShortToast("can not Ransom")
            }

            7 -> {
                showShortToast("Processing")
            }
        }
//        GRansomActivity.startGRansomActivity(mContext)
        return super.onOptionsItemSelected(item)
    }
}